package com.thread;

import java.util.concurrent.*;

public class ThreadService {
    public static void main(String[] args) {
        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1);
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(
                5,
                7,
                1000,
                TimeUnit.MILLISECONDS,
                blockingQueue,
                Thread::new,
                new ThreadPoolExecutor.CallerRunsPolicy()
                );
        Runnable runnable = new RunnableClass();
        ExecutorService pool1 = Executors.newFixedThreadPool(5);
        Callable callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName() + "callable线程正在执行");
                return "测试";
            }
        };
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        Future futureTask = threadPoolExecutor.submit(callable);
        try{
            System.out.println(futureTask.get());
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
